package com.aforce.behaviors;

import java.awt.Component;

import java.awt.Point;
import java.util.Enumeration;

import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.aforce.entities.Ship;
import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
import com.sun.j3d.utils.universe.ViewingPlatform;

public class MouseNavigationBehavior extends MouseBehavior
{
	Point mousePoint = null;
	final Ship ship;
	
	TransformGroup shipTransformGroup;
	TransformGroup eyeTransformGroup = null;
	Transform3D currentTransform = new Transform3D();
	
	public double upDelta = 0;
	public double turnDelta = 0;
	

	final static Point center = new Point(350, 350);

	public MouseNavigationBehavior(Component c, ViewingPlatform view, Ship ship)
	{
		super(c, view.getViewPlatformTransform());
		this.ship = ship;
	}

	public void mousePressed(java.awt.event.MouseEvent e)
	{
		ship.shoot();
	}

	public void mouseMoved(java.awt.event.MouseEvent e)
	{

		// get a copy of the current transform
		ship.getTransformGroup().getTransform(currentTransform);

		mousePoint = e.getPoint();
		double xratio = (mousePoint.x - center.x) / 350.0;
		double yratio = (center.y - mousePoint.y) / 350.0;
		
		//determine up rotation
		applyUpDelta(0.05 * yratio);
		
		
		//determine turn rotation
		applyTurnDelta(0.05 * xratio);

		applyTransforms();
	}
	
	@Override
	public void initialize()
	{
		super.initialize();
		this.wakeupOn(new WakeupOnElapsedFrames(2));
	}

	//TODO: Figure out what this is supposed to do
	protected void applyTransforms()
	{
			//flightBehavior.getForward().scale(1.1);
			//System.out.println("Speed increased to: " + flightBehavior.getForward());
	}

	@Override
	public void processStimulus(Enumeration arg0)
	{
		/*
		this.wakeupOn(new WakeupOnElapsedFrames(2));

		// Spin - allows you to change your turning direction
		{
			AxisAngle4d rotation = new AxisAngle4d(ship.getForwardVector(), 2*turnDelta);
			Transform3D t = new Transform3D();
			t.set(rotation);
			t.transform(ship.getUpVector());
			ship.getUpVector().normalize();
		}
		
		// Rise or Drop - allows you to turn up or down relative to your current orientation
		{
			Vector3d side = new Vector3d();
			side.cross(ship.getForwardVector(), ship.getUpVector());
			side.normalize();
			
			AxisAngle4d rotation = new AxisAngle4d(side, 2*upDelta);
			Transform3D t = new Transform3D();
			t.set(rotation);
			t.transform(ship.getUpVector());
			ship.getUpVector().normalize();
			t.transform(ship.getForwardVector());
			ship.getForwardVector().normalize();
		}
		
		currentTransform.lookAt(new Point3d(0,0,0), new Point3d(ship.getForwardVector()), ship.getUpVector());
		currentTransform.invert();
		currentTransform.normalize();
		currentTransform.setTranslation(new Vector3d(ship.getPosition()));
		ship.getTransformGroup().setTransform(currentTransform);
		*/
	}

	
	public void applyUpDelta(double delta)
	{
		ship.upDelta = delta;
	//	upRotation.angle += delta;
	}
	
	public void applyTurnDelta(double delta)
	{
		ship.turnDelta = delta;
	//	turnRotation.angle -= delta;
	}

}
